package com.cloudknow.manager;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.cloudknow.utils.LogUtils;
//import com.squareup.leakcanary.LeakCanary;

/**
 * Create bu wubin on .
 *
 * @description
 */
public class MyApplication extends Application {
    private static Context mContext;
    private static MyApplication singleton;
    private static ActivityManager activityManager;
    private static Handler handler;
    public static boolean DEBUG_MODE = true;//是否是DEBUG模式

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        singleton = this;

        activityManager = ActivityManager.getInstance();


//        //内存泄漏检测放到最后执行
//        if (LeakCanary.isInAnalyzerProcess(getApplication())) {
//            return;
//        }
//        LeakCanary.install(getInstance());
//        LeakCanary.install(this);

    }

    public static MyApplication getApplication() {
        return singleton;
    }
    public static MyApplication getInstance() {
        return singleton;
    }

    public static Context getContext() {
        return mContext;
    }

    public static ActivityManager getActivityManager() {
        return activityManager;
    }


    /**
     * 判断App是否是Debug版本
     */
    public static boolean isDebug() {
        if (TextUtils.isEmpty(mContext.getPackageName())) {
            return false;
        }
        try {
            PackageManager pm = mContext.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(mContext.getPackageName(), 0);
            return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Handler getHandler() {
        if (null == handler)
            handler = new Handler(Looper.getMainLooper());
        return handler;
    }



    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName() {
        String versionName = "";
        try {
            PackageManager pm = singleton.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(singleton.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            LogUtils.e("VersionInfo Exception", e);
        }
        return versionName;
    }

    /**
     * 返回当前程序版本号
     */
    public static int getAppVersionCode() {
        int versioncode = 0;
        try {
            PackageManager pm = singleton.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(singleton.getPackageName(), 0);
            versioncode = pi.versionCode;
        } catch (Exception e) {
            LogUtils.e("VersionInfo Exception", e);
        }
        return versioncode;
    }





}
